import {
    Component,
    OnInit,
    Injector,
    ViewChild,
    ViewEncapsulation,
} from "@angular/core";
import {
    PlacesServiceProxy,
    UserPlaceListDto,
    ParkingsBillingSchemeServiceProxy,
    ParkingsBillingSchemeListDto,
} from "@shared/service-proxies/service-proxies";
import * as moment from "moment";
import { LazyLoadEvent } from "primeng/components/common/lazyloadevent";
import { Paginator } from "primeng/components/paginator/paginator";
import { Table } from "primeng/components/table/table";

import { finalize } from "rxjs/operators";

import { AppComponentBase } from "@shared/common/app-component-base";
import { appModuleAnimation } from "@shared/animations/routerTransition";
import { ActivatedRoute, Router } from "@angular/router";
import { result } from "lodash";
import { CreateOrEditRenewPlaceModalComponent } from "./create-or-edit-renew-place-modal.component";

@Component({
    selector: "app-park-guard",
    templateUrl: "./userparkings.component.html",
    styleUrls: ["./userparkings.component.css"],
    animations: [appModuleAnimation()],
})
export class UserparkingsComponent extends AppComponentBase implements OnInit {
    @ViewChild("dataTable") dataTable: Table;
    @ViewChild("paginator") paginator: Paginator;
    @ViewChild("createOrEditRenewPlaceModal")
    createOrEditRenewPlaceModal: CreateOrEditRenewPlaceModalComponent;
    constructor(
        injector: Injector,
        private route: Router,
        private activeRoute: ActivatedRoute,
        private _placesServiceProxy: PlacesServiceProxy
    ) {
        super(injector);
    }

    phone: string;
    name: string;
    neighbourhoodName: string;
    address: string;
    userid: number;
    placesId: number;
    parkingID: number;
    UserPlaceLis = new Array<UserPlaceListDto>();
    billingScheme = new ParkingsBillingSchemeListDto();
    ngOnInit() {
        this.activeRoute.queryParams.subscribe((params) => {
            this.phone = params["phone"];
            this.parkingID = Number(params["parkingid"]);
            console.log(params);
            console.log(this.parkingID);
        });
        this.getUserParking();
    }

    getUserParking(event?: LazyLoadEvent): void {
        this._placesServiceProxy
            .getUserParkingInfo(this.phone)
            .pipe(
                finalize(() => this.primengTableHelper.hideLoadingIndicator())
            )
            .subscribe((result) => {
                console.log(result);
                this.UserPlaceLis = result.placeListDtos;
                this.name = result.name;
                this.neighbourhoodName = result.neighbourhoodName;
                this.address = result.address;
                this.userid = result.userId;
            });
    }

    test(userPlaceList: UserPlaceListDto) {
        let time = userPlaceList.endTime;

        this.placesId = userPlaceList.placeId;
        //  this.getParkingsBillingScheme();

        console.log(this.billingScheme);
    }

    // getParkingsBillingScheme() {
    //     this._parkingsBillingSchemeServiceProxy
    //         .getAll(this.placesId, "", 0, 200)
    //         .subscribe((result) => {
    //             console.log(result.items[0]);
    //             this.billingScheme = result.items[0];
    //             console.log(this.billingScheme);
    //         });
    // }

    deleteUserParkingInfo(car:any, userPlaceLis: any){
        this.message.confirm(
            "确定删除车位'" + userPlaceLis.placeName + "'的'" + car.carNum + "'车辆信息吗",            
            (isConfirmed) => {
                if (isConfirmed) {
                    this._placesServiceProxy.deleteUserParkingInfo(car.id,userPlaceLis.placeId)
                    .pipe(
                        finalize(() => this.primengTableHelper.hideLoadingIndicator())
                    )
                    .subscribe((result) => {
                        this.getUserParking();
                    });
                }
            }
        );
    }
}
